<?php
include_once '/var/www/html/new/com.inc.php';

$ac = $GLOBALS['_ac'];
$pw = $GLOBALS['_pw'];
$s  = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));


if($_REQUEST['ac']=='')   exit('{"code":"1011","msg":"请输入授权账号"}');
if($_REQUEST['pw']=='')   exit('{"code":"1011","msg":"请输入授权密码"}');
if($_REQUEST['sid']=='')  exit('{"code":"1011","msg":"请输入景区ID"}');
if($_REQUEST['code']=='') exit('{"code":"1011","msg":"请输入凭证码"}');


$start_time = microtime(true);

// 权限判断
$auth_arr = include '/var/www/html/new/d/module/zj_terminal/zj_permissions.php';
$recv_ac = $_REQUEST['ac'];
$recv_pw = $_REQUEST['pw'];
if(!in_array($recv_ac, $auth_arr)) exit('{"code":"1011","msg":"无授权，拒绝连接"}');

// 获取
// 根据账号生成接口编码
$sql="select dname,id from pft_member where account='$recv_ac' limit 1";
$GLOBALS['le']->query($sql);
if(!$GLOBALS['le']->fetch_assoc()) exit('{"code":"1011","msg":"账号不存在"}');

// http://www.12301.cc/module/zj_terminal/terminalCheck.php?ac=123757&pw=828074ac9f9d648a2eb2992fc33aecb8&sid=616007&code=287205
// http://www.12301.cc/module/zj_terminal/terminalCheck.php?ac=123757&pw=828074ac9f9d648a2eb2992fc33aecb8&sid=617604&code=724465



$dname = $GLOBALS['le']->f('dname');
$memberSID = $GLOBALS['le']->f('id');
$chpw  = $s->Create_Conn_REC($recv_ac, $dname);
if($recv_pw!=$chpw) exit('{"code":"1011","msg":"无授权，拒绝连接"}');

// 订单归属判断
$salerid = $_REQUEST['sid'];
$sel = "select apply_did,terminal,terminal_type from uu_land where salerid='$salerid' limit 1";
$GLOBALS['le']->query($sel);
if(!$GLOBALS['le']->fetch_assoc()) exit('{"code":"1011","msg":"景区不存在"}');
if($memberSID!=$GLOBALS['le']->f('apply_did')) exit('{"code":"4002","msg":"非自供订单，不能验证"}');

$terminal_inum = $GLOBALS['le']->f('terminal');
$terminal_type = $GLOBALS['le']->f('terminal_type');

if($terminal_type==0){// 旧方式验证
	$time = date("Y-m-d H:i:s");
	$result = $s->Terminal_Check_In($salerid, $_REQUEST['code'], null, '');
	if(is_numeric($result)){
		logfile($result.'----runtime---'.microtime(true) - $start_time);
		$repMsg = Err_Word($result);
		exit('{"code":"'.$result.'","msg":"'.$repMsg.'"}');
	}else{
		$obj=@simplexml_load_string($result);
		$ordernum=(string)$obj->Rec->UUordernum;
		$o=$s->Order_Globle_Search(
			$sid, "", "", "", "",
			"", "", "", "", "",
			"", "", $ordernum, "", "", 
			"", "", "", 1, 1, 
			0, 1, 0, 0, "",
			0, "", 0, '', '', 
			"", 0, "",	"", "", "" 	
		); 
		$os=simplexml_load_string($o);
		$UUaids=(string)$os->Rec->UUaids;
		if($UUaids == 0){
			$buyid = (string)$os->Rec->UUbuyid;
		}else{
			$UUaidss= explode(',',$UUaids);
			$buyid = $UUaidss[1];
		}		
		$sql="select dname,account from pft_member where id=".$buyid." limit 1";
		$GLOBALS['le']->query($sql);
		$GLOBALS['le']->fetch_assoc(); 
		$fendname=$GLOBALS['le']->f('dname');
		$account  = $GLOBALS['le']->f('account');
		
		logfile($ordernum."---".$buyid.'----runtime:'.microtime(true) - $start_time);
		exit('{"code":"100","msg":"验证成功111","time":"'.$time.'","fenxiaoshang":"'.$fendname.'","fxsAC":"'.$account.'"}');
	}	
}else{// 新终端服务验证
	include_once('/var/www/html/new/d/class/Terminal_Check_Socket.class.php');
	$tc = Terminal_Check_Socket::connect();
	$chResult = $tc->Terminal_Check_In_Voucher($terminal_inum, $salerid, $_REQUEST['code'],array("vMode"=>5));
	$checkData = json_encode(array($terminal_inum, $salerid, $_REQUEST['code']));
	if($chResult['state']=='success'){
		if(!isset($chResult['multi'])){//代表多单
			// 二级分销商
			$ordernum = $chResult['ordernum'];
			logfile(date('Y-m-d H:i:s')."--".$ordernum);
			$simxml1 = $s->Order_Globle_Search($sid, '', '', '', '', '', '', '', '', '', '', '', $ordernum, '', '', '', '', '', 1, 1, 0, 1, 0, 0, '', 0, '', 0, '', '', '', 0, '', '', '', '');
			$simxml = simplexml_load_string($simxml1);
			$UUaids=(string)$simxml->Rec->UUaids;
			if($UUaids == 0){
				$buyid = (string)$simxml->Rec->UUbuyid;
			}else{
				$UUaidss= explode(',',$UUaids);
				$buyid = $UUaidss[1];
			}
			$sql="select dname,account from pft_member where id=$buyid limit 1";
			$GLOBALS['le']->query($sql);
			$GLOBALS['le']->fetch_assoc(); 
			$fendname = $GLOBALS['le']->f('dname');
			$account  = $GLOBALS['le']->f('account');
			
			$logs = "[$ordernum]订单验证,,数据[$checkData],结果[".json_encode($chResult)."],执行时间[".(microtime(true)-$start_time)."]";
			logfile($logs,'/var/www/html/new/d/module/zj_terminal/terminal_check_new.log');
			exit('{"code":"100","msg":"验证成功","time":"'.date('Y-m-d H:i:s').'","fenxiaoshang":"'.$fendname.'","fxsAC":"'.$account.'"}');
		}
	}
	
	$logs = "订单验证,数据[$checkData],结果[".json_encode($chResult)."],执行时间[".(microtime(true)-$start_time)."]";
	logfile($logs,'/var/www/html/new/d/module/zj_terminal/terminal_check_new_error.log');
	exit('{"code":"101","msg":"'.$chResult['msg'].'"}');
}


function Err_Word($r){
	$errCode = array(
		'105'=>'找不到订单',
		'121'=>'订单状态出错,订单已验证',
		'119'=>'订单状态出错,已取消或其他状态',
		'521'=>'验证间隔时间未到',
		'133'=>'游玩时间未到',
		'134'=>'游玩时间已过',
		'118'=>'终端服务器无响应',
		'1070'=>'订单未支付',
	);
	$eword=$errCode[$r];
	if (!$eword) $eword='其他错误';
	return $eword;
}

function logfile($txt, $file = '/var/www/html/new/d/module/zj_terminal/log.txt'){
	$fp = fopen($file, "a");
	flock($fp, LOCK_EX) ;
	fwrite($fp,$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp); 
}